<?php

namespace app\index\service;

use think\Db;

/**
 * @author xjy
 */
class CountryonwayService
{
    private static $salesState=["1"=>"新产品","2"=>"清仓品","3"=>"在售品","4"=>"下架品"];  //销售状态
    private static $cycleStage=["1"=>"补货期","2"=>"新品期","3"=>"计划淘汰期","4"=>"淘汰期"];  //生命周期

    //国家仓在途明细列表
    static public function getList($param)
    {
        if(empty($param['down'])){
             $titles=[];
        }else{
             $titles=[
                 'code'=>'发运计划单号','local_order_no'=>'物流内单号','sku'=>'SKU','unit_price'=>'出库单价（¥）','quantity'=>'发运数量','warehouse_str_out'=>'发货仓库',
                 'date'=>'发运时间','warequantity'=>'到货数量','warehouse_str_rec'=>'收货国家仓','waredate'=>'收货时间',
                 'differ_num'=>'差异数','differ_money'=>'差异金额（¥）','confirm_status'=>'状态','operator'=>'确认人','operate_time'=>'确认时间'];
        }
        
        return \app\index\model\CountryonwayModel::getList($param,$titles,"app\index\service\CountryonwayService::convertData");  
    }
    public static function convertData($data){
        $warehouse=\app\common\ConfigBase::getWarehouseAll();        
        $localno=\app\common\model\ToolModel::select('ship_plan_logistics',[],'local_order_no','','code');
        $confirm=\app\common\model\ToolModel::select('confirm_countryonway',[],'status,operator,operate_time','','concat(sku,"_",code)');
        if($data){
            foreach($data as $key=>&$value){
                $confirmInfo=isset($confirm[$value['sku']."_".$value['code']])? $confirm[$value['sku']."_".$value['code']]:'';
                $value['warehouse_str_out']=isset($warehouse[$value['warehouse_id_out']]) ? $warehouse[$value['warehouse_id_out']]:'';
                $value['warehouse_str_rec']=isset($warehouse[$value['receipt_warehouse_id']])?$warehouse[$value['receipt_warehouse_id']]:'';
                $value['local_order_no']=isset($localno[$value['code']]) ? $localno[$value['code']]:'';   
                $value['differ_num']=$value['quantity']-$value['warequantity']; 
                $value['differ_money']=$value['differ_num']*$value['unit_price']; 
                $value['status']=!empty($confirmInfo['status']) ?  $confirmInfo['status'] :'1'; 
                $value['confirm_status']=!empty($confirmInfo['status']) && $confirmInfo['status']==2 ? '已确认':'待确认'; 
                $value['operator']=empty($confirmInfo['operator']) ? '':$confirmInfo['operator'];
                $value['operate_time']=empty($confirmInfo['operate_time']) ? '':$confirmInfo['operate_time'];
            }
        }
        $data=arraySort($data,'status',SORT_ASC);
        return $data;  
    }
}
